//导入axios包
import axios from 'axios'
//实例化对象,创建一个axios
const instance = axios.create({
  baseURL: import.meta.env.VITE_VUE_APP_BASE_API,
  timeout: 3000, //
})
// 三,配置拦截器,全局处理
instance.interceptors.request.use(
  //处理请求,Axios拦截器为所有的请求自动设置Authorization header
  (config) => {
    if (localStorage.getItem('token')) {
      config.headers.common['Authorization'] =
        'Bearer ' + localStorage.getItem('token')
    }
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)
instance.interceptors.response.use(
  //处理响应
  (response) => {
    if (response.data.status == 401) {
      return response.data.message
    }
    return response.data
  },
  (error) => {
    return Promise.reject(error)
  }
)
//导出对象
export { instance }
